﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DaoService.Model;
using DaoService.Services;
using SukiUI.Dialogs;
using SysLog;
using UI.Models;
using UI.Utils;
using UI.Views;
using UI.Views.pages;

namespace UI.ViewModels;

public partial class PlcViewModel : ObservableValidator
{
    private PlcInfoService _plcInfoService
    {
        get
        {
            return ServiceProvider.PlcInfoService;
        }
    }

    public PlcInfoVariablesViewModel VariableList { get; set; } = new();
    
    [ObservableProperty] 
    private PlcInfo _plcInfo = new PlcInfo();

    [ObservableProperty] private bool _isChecked;
    private ObservableCollection<PlcInfo> _plcInfoCollection = new ObservableCollection<PlcInfo>();

    public PlcViewModel()
    {
        loadPlcInfos();
    }

    public ObservableCollection<PlcInfo> PlcInfos
    {
        get
        {
            return _plcInfoCollection;
        }
        set
        {
            _plcInfoCollection = value;
            OnPropertyChanged();
        }
    }
    
    public async void loadPlcInfos()
    {
        var result = await _plcInfoService.GetAllAsync();
        foreach (var item in result)
        {
            _plcInfoCollection.Add(PlcInfoMapper.ToModelMapper.Map<PlcInfo>(item));
        }
    }
    
    [RelayCommand]
    public void OpenPlcViewPage()
    {
        PlcInfo = new PlcInfo();
        OpenDialog();
    }

    public void OpenDialog()
    {
        MainWindow.DialogManager.CreateDialog()
            .WithViewModel(dialog => new AddPlcViewPage(dialog,this))
            .TryShow();
    }

    [RelayCommand]
    public async void EditPlcViewPage()
    {
        try
        {
            PlcInfo info = await getCheckedPlcInfo();
            PlcInfo = info;
            OpenDialog();
        }
        catch (Exception e)
        {
            ILog.writeError(e.StackTrace);
        }
    }
    
    [RelayCommand]
    public async Task RemoveItem()
    {
        try
        {
            IsCheckedOn = true;
            PlcInfo info = await getCheckedPlcInfo();
            if (info != null)
            {
                await _plcInfoService.DeleteAsync(info.Id); 
                PlcInfos.Remove(info);
                IsCheckedOn = false;
            }
        }
        catch (Exception e)
        {
            ILog.writeError(e.StackTrace);
            IsCheckedOn = false;
        } 
    }
    
    [RelayCommand]
    private async Task SavePlcInfo()
    {
        // await Task.Delay(3000);
        // Task.Run(async () => { }
        IsCheckedOn = true;
        PlcInfoEntity model = new PlcInfoEntity();
        model = PlcInfoMapper.ToEntityMapper.Map<PlcInfoEntity>(PlcInfo);
        if (PlcInfo.Id == 0)
        {
            model.Id = IntIdGenerator.IntUUID();
            // PlcInfo.Id = model.Id;
            await _plcInfoService.InsertAsync(model);
            PlcInfos.Add(PlcInfo);
        }
        else
        {
            await _plcInfoService.UpdateAsync(model);
        }
        IsCheckedOn = false;
    }

    public async Task<PlcInfo> getCheckedPlcInfo()
    {
        foreach (var info in PlcInfos)
        {
            if (info.IsChecked)
            {
                return info;
            }
        } 
        return null;
    }
    
    public bool IsCheckedOn
    {
        get => IsChecked;
        set
        {
            IsChecked = value;
            OnPropertyChanged(); // 实现 INotifyPropertyChanged
        }
    }
}